package net.noday.persistent.jdbcImpl;

import java.util.List;

import javax.sql.DataSource;

import net.noday.domain.User;
import net.noday.domain.mapping.UserMapping;
import net.noday.persistent.jdbcImpl.mapper.UserMapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import org.springframework.stereotype.Repository;

@Repository("userDAO")//默认名字jdbcUserDAO
public class JdbcUserDAO {

	private JdbcTemplate jdbcTemplate;
	
	@Autowired
	public void setDataSource(DataSource dataSource) {
		this.jdbcTemplate = new JdbcTemplate(dataSource);
	}

	public User loadByUid(int uid) {
		return jdbcTemplate.queryForObject("select * from user where uid = ?", 
				new Object[]{uid}, new UserMapper());
	}

	public Object loadObject(String sql, Object[] param) {
		return (User)jdbcTemplate.queryForObject(sql, param, new UserMapper());
	}

	public int delById(int id) {
		return jdbcTemplate.update("delete from user where uid = ?", 
				new Object[]{id}, new int[]{java.sql.Types.INTEGER});
	}

	public User save(User user) {
		SqlParameterSource parameters = new BeanPropertySqlParameterSource(user);
		Number id = new SimpleJdbcInsert(jdbcTemplate)
			.withTableName(UserMapping.TABLENAME)
			.usingGeneratedKeyColumns(UserMapping.UID)
			.executeAndReturnKey(parameters);
		user.setUid(id.intValue());
		return user;
	}
	public int update(User user) {
		String sql = "update user set " + UserMapping.EMAIL + "=?," + 
		UserMapping.PASSWORD + "=?," + UserMapping.NICKNAME + "=?," + 
		UserMapping.HOMEPAGE + "=?," + UserMapping.ADDRESS + "=?," + 
		UserMapping.EXPERIENCE + "=?," + UserMapping.USERGROUP + "=?," + 
		UserMapping.CREATEDATE + "=?," + UserMapping.CREATEIP + "=?," + 
		UserMapping.ACTIVE + "=?," + UserMapping.LASTLOGINTIME + "=?," + 
		UserMapping.LASTLOGINIP + "=?," + UserMapping.UUID + "=?," + 
		UserMapping.UUIDVALID + "=?," + UserMapping.DESCRIPTION + "=? " + 
		"where " + UserMapping.UID + "=?";
		return jdbcTemplate.update(sql, user.getEmail(), user.getPassword(), 
				user.getNickname(), user.getHomepage(), user.getAddress(), 
				user.getExperience(), user.getUserGroup(), user.getCreateDate(), 
				user.getCreateIp(), user.isActive(), user.getLastLoginTime(), 
				user.getLastLoginIp(), user.getUuid(), user.getUuidValid(), 
				user.getDescription(), user.getUid());
		
	}

	public List<User> listAll() {
		String sql = "select * from user order by uid";
		return jdbcTemplate.query(sql, new UserMapper());
	}

	public List<User> listAllByPage(Object[] params) {
		String sql = "select * from user where uid" + params[0] + "? order by uid " + params[2] + " limit ?,?";
		return jdbcTemplate.query(sql, new UserMapper(), params[1], params[3], params[4]);
	}
	
	public List<User> query(String sql) {
		return jdbcTemplate.query(sql, new UserMapper());
	}
	
	public List<User> query(String sql, Object[] params) {
		return jdbcTemplate.query(sql, params, new UserMapper());
	}

	public int countAll() {
		return jdbcTemplate.queryForInt("select count(*) from user");
	}

	public int countQuery(String sql) {
		return jdbcTemplate.queryForInt(sql);
	}

	public int update(String hql) {
		// TODO Auto-generated method stub
		return 0;
	}

}
